/**
 * Created by 静龄 on 2015/11/9.
 */
/**
 * Created by 静龄 on 2015/11/9.
 */


!
    function(){
        var latestpage=new Rpage.pageObj();
        var warningpage=new Rpage.pageObj();
        warningpage.perpage=20;
        function getdata(display){
            var ischoose=false;
            if(display){
                ischoose=true;
            }
            if(!ischoose || display==1){
                $.post(ALARM_CONTENT_LIST,{"page":latestpage.pagenum,"pageCount":latestpage.perpage},function(data){
                    data= $.parseJSON(data);
                    if(data.code==1){
                        var datalength=data.result.length;
                        for(var i=0;i<datalength;i++){
                            if(data.result[i].task_type==1){
                                $(".latest_panel").append( '' +
                                    '<div class="row">' +
                                    '<div class="index_answer">' +
                                    '<div class="">' +
                                    '<div class="col-lg-1 text-center"><span class="R_switch_a R_read" data-read-id="'+data.result[i].id+'" data-on="已读" data-off="未读" data-ison="'+(data.result[i].uid?"true":"false")+'"></span></div>' +
                                    '<div class="col-lg-3"><i class="fa fa-clock-o"></i>'+Rcheck.changedate(data.result[i].time)+'</div>' +
                                    '<div class="col-lg-1"><i class="fa fa-bookmark"></i>标题</div>' +
                                    '<div class="col-lg-6">'+(data.result[i].url_source.substring(0,data.result[i].url_source.indexOf("-->"))||"")+'</div>' +
                                    '<div class="col-lg-1 tableURL"><a href="'+data.result[i].url_source.substring(data.result[i].url_source.indexOf("-->")-(-3))+'">URL <i class="fa fa-angle-right"></i></a></div>' +
                                    '</div>' +
                                    '<div class="">' +
                                    '<div class="col-lg-12 mouseover_show index_content">' +(function(data,i){
                                        //data.result[i].text_content.substring(data.result[i].text_content.indexOf("-->")-(-3))
                                        var ask=data.result[i].text_content.substring(0,data.result[i].text_content.indexOf("-->"))
                                        if(ask!=""){
                                            ask=ask.replace("【",'<img class="loginlogo" src="'+__ROOT+'/Public/image/q.png"><span class="askpeople">【')
                                            ask=ask.replace("】",'】</span>')
                                        }
                                        return ask;
                                    })(data,i)+
                                    '</div> </div>' +
                                    '<div class="">' +
                                    '<div class="col-lg-12 mouseover_show index_content">' +(function(data,i){
                                        var ask=data.result[i].text_content.substring(data.result[i].text_content.indexOf("-->")-(-3))
                                        //var ask=data.result[i].text_content.substring(0,data.result[i].text_content.indexOf("-->"));
                                        if(ask!=""){
                                            ask=ask.replace("【",'<img class="loginlogo" src="'+__ROOT+'/Public/image/a.png"><span class="askpeople">【')
                                            ask=ask.replace("】",'】</span>')
                                        }
                                        return ask;
                                    })(data,i)+
                                    '</div>' +
                                    '</div>' +
                                    '</div>' +
                                    '</div>');
                            }else{
                                $(".latest_panel").append('' +
                                    '<div class="row">' +
                                    '<div class="index_answer">' +
                                    '<div class="">' +
                                    '<div class="col-lg-1 text-center"><span class="R_switch_a R_read" data-read-id="'+data.result[i].id+'" data-on="已读" data-off="未读" data-ison="'+(data.result[i].uid?"true":"false")+'"></span></div>' +
                                    '<div class="col-lg-3"><i class="fa fa-clock-o"></i>'+Rcheck.changedate(data.result[i].time)+'</div>' +
                                    '<div class="col-lg-1"><i class="fa fa-bookmark"></i>标题</div>' +
                                    '<div class="col-lg-6">'+(data.result[i].title_content||"")+'</div>' +
                                    '<div class="col-lg-1 tableURL"><a href="'+data.result[i].url_source.substring(data.result[i].url_source.indexOf("-->")-(-3))+'">URL <i class="fa fa-angle-right"></i></a></div>' +
                                    '</div>' +
                                    '<div class="index_news_content">' +
                                    '<div class="col-lg-12 mouseover_show index_content">' +data.result[i].text_content+
                                    '</div>' +
                                    '</div>' +
                                    '</div>' +
                                    '</div>');
                            }
                        }
                        latestpage.setallpagenum(data.count);
                        $(".R_switch_a").each(function(index,e){
                            if($(this).attr("data-isset")!="yes"){
                                Rswitch.setswitch($(this));
                                $(this).attr("data-isset","yes");
                            }
                        });
                    }
                });
            }


            if(!ischoose || display==2) {
                $.post(ALARM_LIST, {"page": warningpage.pagenum, "pageCount": warningpage.perpage}, function (data) {
                    data = $.parseJSON(data);
                    if (data.code == 1) {
                        var datalength = data.result.length;
                        for (var i = 0; i < datalength; i++) {
                            $(".index-warningtable").children("tbody").append('<tr class="index_warning_tr">' +
                                '<td><span class="R_switch_a R_read" data-read-id="' + data.result[i].id + '" data-on="已读" data-off="未读" data-ison="' + (data.result[i].uid ? "true" : "false") + '"></span></td>' +
                                '<td>' + (data.result[i].rule_alert || "") + '</td>' +
                                '<td><a target="_blank" href="'+data.result[i].url_source.substring(data.result[i].url_source.indexOf("-->")-(-3))+'"><div class="mouseover_show">' + (function(data,i){
                                    if(data.result[i].title_content){
                                        return (data.result[i].title_content)
                                    }else{
                                        var ask1=data.result[i].text_content.substring(0,data.result[i].text_content.indexOf("-->"));
                                        if(ask1!=""){
                                            ask1=ask1.replace("【",'<span class="askpeople">【')
                                            ask1=ask1.replace("】",'】</span>')
                                        }

                                        var ask2=data.result[i].text_content.substring(data.result[i].text_content.indexOf("-->")-(-3));
                                        //var ask=data.result[i].text_content.substring(0,data.result[i].text_content.indexOf("-->"))
                                        if(ask2!=""){
                                            ask2=ask2.replace("【",'<span class="askpeople">【')
                                            ask2=ask2.replace("】",'】</span>')
                                        }
                                        var ask=ask1+"<hr>"+ask2;
                                        return ask;
                                    }

                                })(data,i) + '</div></a></td>' +
                                '</tr>');
                        }

                        warningpage.setallpagenum(data.count);
                        $(".R_switch_a").each(function (index, e) {
                            if($(this).attr("data-isset")!="yes"){
                                Rswitch.setswitch($(this));
                                $(this).attr("data-isset","yes");
                            }
                        });
                    }
                });
            }


            if(!ischoose || display==0){
                $.post(TASK_DATA_COUNT,{"task_type":1},function(data){
                    data= $.parseJSON(data);
                    if(data.code==1){
                        $(".index_msg_1").children("div").children("lable:eq(1)").html(data.count.count_thread||0);
                        $(".index_msg_1").children("div").children("lable:eq(2)").html(data.count.count_success||0);
                        $(".index_msg_1").children("div").children("lable:eq(3)").html(data.count.count_failed||0);
                    }
                });
                $.post(TASK_DATA_COUNT,{"task_type":2},function(data){
                    data= $.parseJSON(data);
                    if(data.code==1){
                        $(".index_msg_2").children("div").children("lable:eq(1)").html(data.count.count_thread||0);
                        $(".index_msg_2").children("div").children("lable:eq(2)").html(data.count.count_success||0);
                        $(".index_msg_2").children("div").children("lable:eq(3)").html(data.count.count_failed||0);
                    }
                });

                $.post(TASK_COUNT,{"task_type":1},function(data){
                    data= $.parseJSON(data);
                    if(data.code==1){
                        $(".index_msg_1").children("div").children("lable:eq(0)").html(data.count);
                    }
                });
                $.post(TASK_COUNT,{"task_type":2},function(data){
                    data= $.parseJSON(data);
                    if(data.code==1){
                        $(".index_msg_2").children("div").children("lable:eq(0)").html(data.count);
                    }
                });


                $.post(TASK_VALUE_DATA_COUNT,{"task_type":1},function(data){
                    data= $.parseJSON(data);
                    if(data.code==1){
                        $(".index_msg_1").children("div").children("lable:eq(4)").html(data.count);
                    }
                });
                $.post(TASK_VALUE_DATA_COUNT,{"task_type":2},function(data){
                    data= $.parseJSON(data);
                    if(data.code==1){
                        $(".index_msg_2").children("div").children("lable:eq(4)").html(data.count);
                    }
                });

                $.post(ALL_ANSWER_COUNT,{"task_type":1},function(data){
                    data= $.parseJSON(data);
                    if(data.code==1){
                        $(".index_msg_3").children("div:eq(0)").children("lable:eq(1)").html(data.count);
                    }
                });
                $.post(ALL_ANSWER_COUNT,{"task_type":2},function(data){
                    data= $.parseJSON(data);
                    if(data.code==1){
                        $(".index_msg_3").children("div:eq(1)").children("lable:eq(1)").html(data.count);
                    }
                });
                $.post(ALL_ANSWER_COUNT,{"task_type":0},function(data){
                    data= $.parseJSON(data);
                    if(data.code==1){
                        $(".index_msg_3").children("div:eq(2)").children("lable:eq(1)").html(data.count);
                    }
                });
                $.post(NOW_ACSWER_COUNT,{"task_type":0},function(data){
                    data= $.parseJSON(data);
                    $(".index_msg_3").children("div:eq(2)").children("lable:eq(0)").html(data.count);

                });
                $.post(NOW_ACSWER_COUNT,{"task_type":1},function(data){
                    data= $.parseJSON(data);
                    $(".index_msg_3").children("div:eq(0)").children("lable:eq(0)").html(data.count);

                });
                $.post(NOW_ACSWER_COUNT,{"task_type":2},function(data){
                    data= $.parseJSON(data);
                    $(".index_msg_3").children("div:eq(1)").children("lable:eq(0)").html(data.count);

                });
            }


        }
        getdata();


        //window.setInterval(function(){
        //    getdata(0);
        //},3000);
        latestpage.setpagediv($(".table_page"));
        warningpage.setpagediv($(".table_page2"));


        latestpage.setisshowpagediv(false);
        warningpage.setisshowpagediv(false);

        var latest_news=new Rfall.Rfallobj($(".latest_panel"),function(){
            if(latestpage.pagenum<latestpage.allpagenum){
                latestpage.pagenum++;
                getdata(1);
            }
        });

        var warning_newss=new Rfall.Rfallobj($(".index-warningtable"),function(){
            if(warningpage.pagenum<warningpage.allpagenum){
                warningpage.pagenum++;
                getdata(2);
            }
        });





        $(".indexlatestallread").click(function(e){
            $(".index_answer").children("div").children("div").children(".R_switch_a").each(function(index,e){
                var jqobj=$(this);
                if(jqobj.attr("data-ison")=="false"){
                    jqobj.click();
                }
                $.post(UPDATE_ANSWER,{"ccid":jqobj.attr("data-read-id"),"isRead":1});
            });
        });


        $(".indexwarningallread").click(function(e){
            $(".index-warningtable").children("tbody").children("tr").children("td").children(".R_switch_a").each(function(index,e){
                var jqobj=$(this);
                if(jqobj.attr("data-ison")=="false"){
                    jqobj.click();
                }
                $.post(UPDATE_ANSWER,{"ccid":jqobj.attr("data-read-id"),"isRead":1});
            });
        });


    }();